//
//  YCHomeViewController.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/15.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit

class YCHomeViewController: YCBasicViewController {

    var status: [YCStatus]?
    
    private var identifer = "statusCell"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addBarButtonItems()
            YCStatus.loadStatusData({ (result, error) -> Void in
                self.status = result!
                self.tableView.reloadData()
            })
        //注册cell
        tableView.registerClass(YCStatusCell.self, forCellReuseIdentifier: identifer)
        
        // 设置cell预估行高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = 200
        // 设置cell行高根据约束来自适应
        tableView.rowHeight = UITableViewAutomaticDimension
        
        // 去掉cell的分割线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
    
    }

    
    
    
    private func addBarButtonItems(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_pop")
        
        //加载我的名字
        let titleName = YCUserAccount.loadUserAccount()?.screen_name ?? "没有加载到数据"
        let titleBtn =  YCTitleButtonView(title: titleName, titleImageName: "navigationbar_arrow_down")
       
       titleBtn.addTarget(self, action: "titleBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
         navigationItem.titleView = titleBtn
    }
    
    func titleBtnClick(titleBtn: YCTitleButtonView){
    
        titleBtn.selected = !titleBtn.selected
        
        if titleBtn.selected {
        
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                titleBtn.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01))
            })
            
         
        }else{
            
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                 titleBtn.imageView?.transform = CGAffineTransformIdentity
            })
           
        
        }
       
    }
    
    // MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return status?.count ?? 0
        
        
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCellWithIdentifier(identifer) as? YCStatusCell

        cell?.status = status![indexPath.row]
        
        return cell!
        
    }
    
//    MARK: - 懒加载
//    lazy var status:[YCStatus] = {
//        var statu = [YCStatus]()
//        YCStatus.loadStatusData({ (result, error) -> Void in
//            
//            statu = result!
//            self.tableView.reloadData()
//        })
//        
//        print("statustatustatustatu\(statu.count)")
//        return statu
//    }()


}
